package com.gwsdk.paysdk.http.request;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.gwsdk.paysdk.bean.VerifyCode;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.http.RequestUtil;
import com.gwsdk.paysdk.utils.ConfigureApp;
import com.gwsdk.paysdk.utils.GWLog;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONObject;

public class CodeRequest {

    public static boolean LOGIN_NETWORK_ERROR;
    private static final String TAG = "CodeRequest";

    HttpUtils http;
    Handler mHandler;


    public CodeRequest(Handler mHandler) {
        http = new HttpUtils();
        if (null != mHandler) {
            this.mHandler = mHandler;
        }
    }

    public void post(final String url, RequestParams params, final Context context) {
        LOGIN_NETWORK_ERROR = false;
        if(!ConfigureApp.getInstance().Configure(context)){
            return;
        }
        if (TextUtils.isEmpty(url) || null == params) {
            GWLog.e(TAG, "fun#post url is null add params is null");
            noticeResult(Constant.LOGIN_FAIL, "参数为空"+params+"==========="+url);
            return;
        }
        GWLog.w(TAG, "fun#post url = " + url);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String response= RequestUtil.getResponseAndUrl(responseInfo, url);
                GWLog.e(TAG, "msg:" + response);
                int status;
                try {
                    JSONObject json = new JSONObject(response);
                    status=json.optInt("code");
                    VerifyCode verifyCode = new VerifyCode();
                    if(status==200){
                        verifyCode.setCode("");
                        noticeResult(Constant.VERIFYCODE_REQUEST_SUCCESS, verifyCode);
                    }else{
                        String msg = json.optString("msg");
                        GWLog.e(TAG, "msg:" + msg);
                        noticeResult(Constant.VERIFYCODE_REQUEST_FAIL, msg);
                    }
                }  catch (Exception e) {
                    GWLog.e(TAG, e.toString());
                    noticeResult(Constant.VERIFYCODE_REQUEST_FAIL, "解析数据异常");
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                GWLog.e(TAG, "onFailure" + msg);
                GWLog.e(TAG, "onFailure" + error.getExceptionCode()+error.getStackTrace()+error.getMessage());
            }
        });
    }

    private void noticeResult(int type, Object obj) {
        Message msg = new Message();
        msg.what = type;
        msg.obj = obj;
        if (null != mHandler) {
            mHandler.sendMessage(msg);
        }
    }


}
